﻿using CPFrameWork.Global;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;

namespace BusinessManage.AccountDept
{
    public class AccountDeptController: CPWebApiBaseController
    {

        [HttpPost]
        public CPWebApiBaseReturnEntity SaveAccountDept([FromBody] AccountDeptModel accountDeptModel)
        {
            CPWebApiBaseReturnEntity re = new CPWebApiBaseReturnEntity();
            try
            {
                int CurUserId = Convert.ToInt32(accountDeptModel.CurUserId);
                string CurUserIden = accountDeptModel.CurUserIden;
                string accountUserId = accountDeptModel.AccountUserId;//区域会计用户编号
                string depIds = accountDeptModel.DepIds;              //销售部编号（多个以逗号分隔）
                if (this.CheckUserIden(CurUserId, CurUserIden) == false)
                {
                    re.Result = false;
                    re.ErrorMsg = "登陆超时，请退出重新登陆！";
                    return re;
                }
                AccountDept accountDept = new AccountDept();
                //1、先删除此区域会计与销售部关系的数据
                accountDept.DeleteAccountDeptRelation(accountUserId);
                //2、再保存区域会计与销售部之间的关系               
                string[] relationArray = new string[0];
                if (depIds != string.Empty) relationArray = depIds.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < relationArray.Length; i++)
                {
                    re = accountDept.SaveAccountDeptRelation(accountUserId, relationArray[i].ToString());
                }
                return re;
            }
            catch (Exception ex)
            {
                re.Result = false;
                re.ErrorMsg = ex.Message.ToString();
                return re;
            }
        }
    }
}
